home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-04-22 | 6.9 KB | 251 lines | [TEXT/MPS ] |
- /*
- File: OSLGSExm.c (Orignal name: OSLGetSetExmn.c)
-
- Contains: OSL Globals
-
- Owned by: Nick Pilch
-
- Copyright: © 1993 - 1995 by Apple Computer, Inc., all rights reserved.
-
- Change History (most recent first):
-
- <7> 6/27/95 NP 1262792: Fix stack dynamic allocation
- problem
- <6> 6/24/95 TJ Changed kNumStaticContextStackEntries from
- a const to a #define so it would compile
- with SC.
- <5> 6/23/95 NP 1195474: Make Resolve reentrant.
- <4> 2/22/95 eeh 1222904: fix use of SetCurrentContext
- <3> 2/8/95 NP 1218550: Don't allocate OSLContexts
- dynamically.
- <2> 8/19/94 NP 1181622: Ownership fix.
- <6> 5/5/94 eeh bug #1160654: fix for SCPP
- <5> 5/2/94 eeh bug #1160654: various PPC native changes
- <4> 8/18/93 NP Added get and set current context.
- <3> 7/29/93 NP Removed compiler warning for unused
- parameters.
- <2> 7/28/93 NP Mods for new token type, OSLToken.
- <1> 7/21/93 NP first checked in
-
- To Do:
- */
-
- // ASSUMPTION: This code will be used in a library that has per-context globals!
-
- ///////////////////////////////////////////////////////////////////////////////
- // ©Apple Computer, Inc. 1992
- // All Rights Reserved.
- // Author: Eric House
- ///////////////////////////////////////////////////////////////////////////////
-
-
- #include "OSLPriv.h"
-
-
- #define kExmnKey1 'eone'
- #define kExmnKey2 'etwo'
-
- #define UNUSED(x) ((void) &x)
-
- // this guy should go away, and be replaced by calls to GetAccessor or
- // GetEventHandler using the keys defined above. This should allow a
- // more general storage mechanism if it becomes necessary.
- // static AEDesc theGlobal ;
-
- OSLToken theGlobalToken;
-
- static OSErr
- GetGlobalDesc( unsigned long key1, unsigned long key2, AEDesc* desc )
- {
- // return iAEGetObjectAccessor( key1, key2, (accessorProcPtr *)&desc->descriptorType,
- // (long *)&desc->dataHandle, false, 0) ;
- // *desc = theGlobalDesc;
- UNUSED( key1 );
- UNUSED( key2 );
- UNUSED( desc );
- return noErr;
- }
-
- static OSErr
- SetGlobalDesc( unsigned long key1, unsigned long key2, AEDesc* desc )
- {
- // return iAEInstallObjectAccessor( key1, key2, (accessorProcPtr)desc->descriptorType,
- // (long)desc->dataHandle, false, 0) ;
- // theGlobalDesc = *desc;
- UNUSED( key1 );
- UNUSED( key2 );
- UNUSED( desc );
- return noErr;
- }
-
-
- // Get the currently available "global" exmn token
- OSErr
- GetExmn( OSLToken *result )
- {
- *result = theGlobalToken;
- return noErr;
- // return GetGlobalDesc( kExmnKey1, kExmnKey2, result ) ;
- }
-
- // Set the "global" exmn token to this one
- OSErr
- SetExmn( OSLToken *newDesc )
- {
- theGlobalToken = *newDesc;
- return noErr;
- // return SetGlobalDesc( kExmnKey1, kExmnKey2, newDesc ) ;
- }
-
-
- // Replace the currently available "global" exmn token with
- // the one provided here. And return the old value in its
- // place.
- OSErr
- SwapExmn( AEDesc *oldAndNewDesc )
- {
- AEDesc temp ;
- OSErr err = GetGlobalDesc( kExmnKey1, kExmnKey2, &temp ) ;
- if ( err == noErr )
- {
- err = SetGlobalDesc( kExmnKey1, kExmnKey2, oldAndNewDesc ) ;
- if ( err == noErr )
- *oldAndNewDesc = temp ;
- }
- return err ;
- }
-
- //==============================================================================
- // Context stack
- //
- // Maintain stack of current contexts, one for each time OSLResolve is called.
- //==============================================================================
-
- #define kNumStaticContextStackEntries 5
-
- struct ContextStack
- {
- unsigned short numEntries;
- OSLContext contextArray[kNumStaticContextStackEntries];
- unsigned short bufferSize;
- OSLContext* buffer;
- };
- typedef struct ContextStack ContextStack;
-
- ContextStack gContextStack =
- {false, {{NULL, 0}, {NULL, 0}, {NULL, 0}, {NULL, 0}, {NULL, 0}}, 0, NULL};
-
- //------------------------------------------------------------------------------
- // AddContextToTopOfStack
- //
- // Use static entries in structure until they are filled up and then do
- // dynamic allocation. We don't ever shrink the dynamic structure, we only
- // grow it.
- //------------------------------------------------------------------------------
-
- OSErr AddContextToTopOfStack(OSLContext* context)
- {
- OSErr error = noErr;
-
- #if ODDEBUG
- // WE ALWAYS HAVE AT LEAST ONE THAT'S PUT ON THE STACK BY iAEObjectInit
- if (gContextStack.numEntries == 2)
- DebugStr("\pSomeone is actually calling Resolve reentrantly!");
- #endif
-
- if (gContextStack.numEntries < kNumStaticContextStackEntries)
- gContextStack.contextArray[gContextStack.numEntries] = *context;
- else
- {
- // THE FIRST TIME WE ARE CALLED TO ALLOCATE ONE MORE CONTEXT THAN
- // kNumStaticContextStackEntries
- if (gContextStack.buffer == NULL)
- {
- gContextStack.buffer = (OSLContext*)NewPtr(sizeof(OSLContext));
- if (error == noErr)
- {
- if (gContextStack.buffer == NULL)
- error = memFullErr;
- }
- if (error == noErr)
- gContextStack.bufferSize = sizeof(OSLContext);
- }
- else
- {
- Size newBufferSize =
- (gContextStack.numEntries - kNumStaticContextStackEntries
- + 1) * sizeof(OSLContext);
- if (gContextStack.bufferSize < newBufferSize)
- {
- OSLContext* newBuffer = (OSLContext*)NewPtr(newBufferSize);
- error = MemError();
- if (error == noErr)
- {
- BlockMoveData(gContextStack.buffer, newBuffer,
- gContextStack.bufferSize);
- DisposePtr((Ptr)gContextStack.buffer);
- gContextStack.buffer = newBuffer;
- gContextStack.bufferSize = newBufferSize;
- }
- }
- }
- if (error == noErr)
- *(gContextStack.buffer
- + (gContextStack.numEntries - kNumStaticContextStackEntries)) = *context;
- }
-
- if (error == noErr)
- ++gContextStack.numEntries;
-
- return error;
- }
-
- //------------------------------------------------------------------------------
- // RemoveTopOfContextStack
- //------------------------------------------------------------------------------
-
- OSErr RemoveTopOfContextStack()
- {
- --gContextStack.numEntries;
- return noErr;
- }
-
- //OSLContext theGlobalContext;
-
- //------------------------------------------------------------------------------
- // GetCurrentContext
- //------------------------------------------------------------------------------
-
- OSErr GetCurrentContext( OSLContext* curContext )
- {
- // *curContext = theGlobalContext;
- if (gContextStack.numEntries <= kNumStaticContextStackEntries)
- *curContext = gContextStack.contextArray[gContextStack.numEntries - 1];
- else
- {
- *curContext =
- *(gContextStack.buffer
- + (gContextStack.numEntries - kNumStaticContextStackEntries - 1));
- }
- return noErr;
- }
-
- //------------------------------------------------------------------------------
- // SetCurrentContext
- //------------------------------------------------------------------------------
-
- OSErr SetCurrentContext( OSLContext* curContext )
- {
- // theGlobalContext = *curContext;
- if (gContextStack.numEntries <= kNumStaticContextStackEntries)
- gContextStack.contextArray[gContextStack.numEntries - 1] = *curContext;
- else
- {
- *(gContextStack.buffer
- + (gContextStack.numEntries - kNumStaticContextStackEntries - 1))
- = *curContext;
- }
- return noErr;
- }
-
-